﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class UserAvatar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = string.Empty;

            string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
            string Output = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Output]);
            string Command = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Command]);

            if (!string.IsNullOrWhiteSpace(UserName))
            {
                ZhangZhao.BLL.H_UserAvatar bll = new ZhangZhao.BLL.H_UserAvatar();

                if (string.IsNullOrWhiteSpace(Command) || !Command.Equals(CommandParameterValue.All, StringComparison.CurrentCultureIgnoreCase))
                {
                    #region 返回当前有效的头像
                    ZhangZhao.Model.H_UserAvatar UserAvatar = bll.GetModel(UserName);

                    if (UserAvatar != null)
                    {
                        if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                        {
                            s = JsonConvert.SerializeObject(UserAvatar);
                        }
                        else
                        {
                            using (MemoryStream Stream = new MemoryStream())
                            {
                                XmlSerializer xml = new XmlSerializer(typeof(ZhangZhao.Model.H_UserAvatar));

                                try
                                {
                                    xml.Serialize(Stream, UserAvatar);
                                }
                                catch { }

                                Stream.Position = 0;
                                using (StreamReader sr = new StreamReader(Stream))
                                {
                                    s = sr.ReadToEnd();
                                }

                                xml = null;
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    #region 返回全部头像图片
                    List<ZhangZhao.Model.H_UserAvatar> list = bll.GetModelList(string.Format(" UserName = '{0}' ", UserName));
                    if (list != null && list.Count > 0)
                    {
                        if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                        {
                            s = JsonConvert.SerializeObject(list);
                        }
                        else
                        {
                            using (MemoryStream Stream = new MemoryStream())
                            {
                                XmlSerializer xml = new XmlSerializer(typeof(List<ZhangZhao.Model.H_UserAvatar>));

                                try
                                {
                                    xml.Serialize(Stream, list);
                                }
                                catch { }

                                Stream.Position = 0;
                                using (StreamReader sr = new StreamReader(Stream))
                                {
                                    s = sr.ReadToEnd();
                                }

                                xml = null;
                            }
                        }
                    }
                    #endregion
                }
            }

            Response.Clear();
            Response.Write(s);
            Response.End();
        }
    }
}